<?php
namespace Admin\Model;
use Think\Model;
class UserModel extends Model 
{
	protected $insertFields = array('username','form','telphone','time','code','charge','remarks','company_id');
	protected $updateFields = array('username','form','telphone','time','code','charge','remarks');
	protected $_validate = array(
		array('username', '1,30', '学生姓名的值最长不能超过 30 个字符！', 2, 'length', 3),
		array('telphone', '1,50', '学生电话的值最长不能超过 50 个字符！', 2, 'length', 3),
	);
	public function search($pageSize = 8)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($username = I('get.username'))
			$where['username'] = array('like', "%$username%");
        if($username = I('post.username'))  //
            $where['username'] = array('like', "%$username%");
		if($studentId = I('get.studentId'))
			$where['studentid'] = array('eq', $studentId);
		if($telphone = I('post.telphone'))   //
			$where['telphone'] = array('like', "%$telphone%");
		if($telphone = I('get.telphone'))
			$where['telphone'] = array('like', "%$telphone%");
		if($experience = I('get.experience'))
			$where['experience'] = array('eq', $experience);
		if($qq = I('get.qq'))
			$where['qq'] = array('like', "%$qq%");
        //对应瑜伽馆
        if(intval(session('admin.company_id')) !==1){
            $where['company_id'] = ['eq',session('admin.company_id')];
        }
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->alias('a')->where($where)->group('a.id')->limit($page->firstRow.','.$page->listRows)->order('')->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
        $data['time'] = date("Y-m-d H:i:s");
        $data['company_id'] = session('admin.company_id');
    }
	// 修改前
	protected function _before_update(&$data, $option)
	{
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
	}
	protected function _after_insert($option){
//		$model = D('Member');
	}
	/************************************ 其他方法 ********************************************/
}